R Programming Tips


Preamble

Over the past couple of years, I’ve had the privilege to advance my R skills, as well as acquire useful functions that should aid anyone using R for behavioral science. This list is not exhaustive, but a list of my most used functions, packages, and useful tips!

Pipe %>% Operator

The pipe operator, written as %>% takes the output of one function and passes it into another function as an argument. This allows us to link a sequence of analysis steps.

For a mathematical analogy, f(x) can be rewritten as x %>% f

## compute the logarithm of `x`

x <- 1

log(x)
## [1] 0
## compute the logaritm of `x`

x %>% log()
## [1] 0

Why is this useful though? R is a functional language, which means that your code often contains a lot of parenthesis, ( and ). When you have complex code, this often will mean that you will have to nest those parentheses together. This makes your R code hard to read and understand.

# Initialize `x`
x <- c(0.109, 0.359, 0.63, 0.996, 0.515, 0.142, 0.017, 0.829, 0.907)

# Compute the logarithm of `x`, return suitably lagged and iterated differences, 
# compute the exponential function and round the result
round(exp(diff(log(x))), 1)
## [1]  3.3  1.8  1.6  0.5  0.3  0.1 48.8  1.1
# Compute the same computation as above but with pipe in operator

x %>% log() %>%
    diff() %>%
    exp() %>%
    round(1)
## [1]  3.3  1.8  1.6  0.5  0.3  0.1 48.8  1.1

In short, here are four reasons why you should be using pipes in R:

  1. You’ll structure the sequence of your data operations from left to right, as apposed to from inside and out;

  2. You’ll avoid nested function calls;

  3. You’ll minimize the need for local variables and function definitions;

  4. You’ll make it easy to add steps anywhere in the sequence of operations.

dplyr package

By far my most used package in R is dplyr. See documentation here